package com.ysq.www.common.utils.client;

import com.alibaba.fastjson.JSONObject;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.util.EntityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * @author dql
 * @Description 外部链接工具类
 * @Date 2020/9/16 0016
 */
public class ClientUtils {

    private static Logger logger = LoggerFactory.getLogger(ClientUtils.class);

    //H5网页授权获取access_token
    public static JSONObject getH5AccessToken(String appId, String APPSecret, String jsCode){
        //拼接地址
        StringBuffer sbUrl = new StringBuffer("https://api.weixin.qq.com/sns/oauth2/access_token?");
        sbUrl.append("appid=")
                .append(appId)
                .append("&secret=")
                .append(APPSecret)
                .append("&code=")
                .append(jsCode)
                .append("&grant_type=authorization_code");
        JSONObject res = null;
        try {
            HttpClient client = HttpClientBuilder.create().build();//构建一个Client
            HttpGet get = new HttpGet(sbUrl.toString());//构建一个GET请求
            HttpResponse response = client.execute(get);//提交GET请求
            HttpEntity result = response.getEntity();//拿到返回的HttpResponse的"实体"
            String r = EntityUtils.toString(result);
            System.out.println(r);//打印返回的信息
            res = JSONObject.parseObject(r);
        }catch (Exception e){
            e.printStackTrace();
            return res;
        }
        return res;
    }




}
